﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.DataManagement.Interfaces;
using ICeFramework.DataManagement.Collections;
using Microsoft.SqlServer.Management.Smo;

namespace ICeFramework.DataManagement.MSSql
{
    public class MSSqlDatabase : IDatabase
    {
        private Database _database;
        #region public MSSqlDatabase(Database database)
        /// <summary>
        /// Initializes a new instance of the <b>MSSqlDatabase</b> class.
        /// </summary>
        /// <param name="database"></param>
        public MSSqlDatabase(IServer parent, Database database)
        {
            //_activeConnections = database.ActiveConnections;
            _name = database.Name;
            _owner = database.Owner;
            _parent = parent;
            _userName = database.UserName;
            _database = database;
        }
        #endregion

        public void RefreshTables()
        {
            if (_database == null)
            {
                return;
            }
            _tables.Clear();
            foreach (Table tbl in _database.Tables)
            {
                if (!tbl.IsSystemObject)
                {
                    ITable mssqlTbl = new MSSqlTable(tbl);
                    _tables.Add(mssqlTbl);
                }
            }
        }

        public void RefreshViews()
        {
            if (_database == null)
            {
                return;
            }
            _views.Clear();
            foreach (View view in _database.Views)
            {
                if (!view.IsSystemObject)
                {
                    IView mssqlView = new MSSqlView(view);
                    _views.Add(mssqlView);
                }
            }
        }

        #region public override string ToString()
        /// <summary>
        /// Returns a <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlDatabase"/>.
        /// </summary>
        /// <returns>A <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlDatabase"/>.</returns>
        public override string ToString()
        {
            return _name;
        }
        #endregion

        //private int _activeConnections = 0;
        //#region public int ActiveConnections
        ///// <summary>
        ///// Gets the ActiveConnections of the MSSqlDatabase
        ///// </summary>
        ///// <value></value>
        //public int ActiveConnections
        //{
        //    get { return _activeConnections; }
        //}
        //#endregion

        private string _name;
        #region public string Name
        /// <summary>
        /// Get/Sets the Name of the MSSqlDatabase
        /// </summary>
        /// <value></value>
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        #endregion

        private string _owner;
        #region public string Owner
        /// <summary>
        /// Get/Sets the Owner of the MSSqlDatabase
        /// </summary>
        /// <value></value>
        public string Owner
        {
            get
            {
                return _owner;
            }
            set
            {
                _owner = value;
            }
        }
        #endregion

        private IServer _parent;
        #region public IServer Parent
        /// <summary>
        /// Gets the Parent of the MSSqlDatabase
        /// </summary>
        /// <value></value>
        public IServer Parent
        {
            get { return _parent; }
        }
        #endregion

        private SqlTableList _tables = new SqlTableList();
        #region public SqlTableList Tables
        /// <summary>
        /// Get/Sets the Tables of the MSSqlDatabase
        /// </summary>
        /// <value></value>
        public SqlTableList Tables
        {
            get
            {
                return _tables;
            }
        }
        #endregion

        private SqlViewList _views = new SqlViewList();
        #region public SqlViewList Views
        /// <summary>
        /// Gets the Views of the MSSqlDatabase
        /// </summary>
        /// <value></value>
        public SqlViewList Views
        {
            get { return _views; }
        }
        #endregion

        private string _userName;
        #region public string UserName
        /// <summary>
        /// Gets the UserName of the MSSqlDatabase
        /// </summary>
        /// <value></value>
        public string UserName
        {
            get
            {
                return _userName;
            }
        }
        #endregion
    }
}
